package com.yitong.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.codehaus.xfire.client.Client;


public class TuiSongUtils {
	//推送json给http接口
	public static String tuiSong(String s,String url) throws ClientProtocolException, IOException{ 
		System.out.println("send json: "+s);
		String result=null;
		
		StringEntity entity = new StringEntity(s, ContentType.create("text/json", "UTF-8"));
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(url);
		httppost.setEntity(entity);
		CloseableHttpResponse response = httpclient.execute(httppost);
		if (response.getStatusLine().getStatusCode() == 200) {
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				BufferedReader bufferedReader=null;
				bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(),"UTF-8"), 8 * 1024);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					sb.append(line);
				}
				System.out.println("recv json: " + sb.toString());
				result = sb.toString();
			}
		}
		response.close();
			
		return result;
	}
	
	//推送xml给webservice接口
	public static Object[] xfire(String name,String url,String paramStr)throws Exception{
		System.out.println("send xml: "+paramStr);
		Client client = new Client(new URL(url));
		Object[] results = client.invoke(name, new Object[] { paramStr});
		System.out.println("recv xml: " + results.toString());
		return results;
	}

}
